<template>
   <div class="app-container">
      <a-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" layout="inline">
         <a-form-item label="部门名称" field="deptName">
            <a-input
               v-model="queryParams.deptName"
               placeholder="请输入部门名称"
               clearable
               @keyup.enter="handleQuery"
            />
         </a-form-item>
         <a-form-item label="状态" field="status">
            <a-select v-model="queryParams.status" placeholder="部门状态" style="width: 200px" clearable>
               <a-option
                  v-for="dict in sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </a-select>
         </a-form-item>
         <a-form-item>
            <a-button type="primary" class="button-margin-right" @click="handleQuery">
              <template #icon>
                <icon-Search/>
              </template>
              搜索
            </a-button>
            <a-button  @click="resetQuery">
              <template #icon>
                <icon-refresh />
              </template>
              重置
            </a-button>
         </a-form-item>
      </a-form>

      <a-row :gutter="10" class="mb8">
         <a-col :span="1.5">
            <a-button
               type="primary"
               plain
               @click="handleAdd"
               v-hasPermi="['system:dept:add']"
            >
              <template #icon>
                <icon-plus />
              </template>
              新增
            </a-button>
         </a-col>
         <a-col :span="1.5">
            <a-button
               status="normal"
               plain
               @click="toggleExpandAll"
            >
              <template #icon>
                <icon-mind-mapping />
              </template>
              展开/折叠
            </a-button>
         </a-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </a-row>

      <a-table
         v-if="refreshTable"
         v-loading="loading"
         :data="deptList"
         row-key="deptId"
         :default-expand-all-rows="isExpandAll"
         :pagination="false"
      >
        <template #columns>
          <a-table-column data-index="deptName" title="部门名称" width="260"></a-table-column>
          <a-table-column data-index="orderNum" title="排序" width="200"></a-table-column>
          <a-table-column data-index="status" title="状态" width="100">
            <template #cell="{record}">
              <dict-tag :options="sys_normal_disable" :value="record.status" />
            </template>
          </a-table-column>
          <a-table-column title="创建时间" align="center" data-index="createTime" width="200">
            <template #cell="{record}">
              <span>{{ parseTime(record.createTime) }}</span>
            </template>
          </a-table-column>
          <a-table-column title="操作" align="center" class-name="small-padding fixed-width">
            <template #cell="{record}">
              <a-button
                  type="text"
                  @click="handleUpdate(record)"
                  v-hasPermi="['system:dept:edit']"
              >
                <icon-edit/>
                修改
              </a-button>
              <a-button
                  type="text"
                  @click="handleAdd(record)"
                  v-hasPermi="['system:dept:add']"
              >
                <icon-plus/>
                新增
              </a-button>
              <a-button
                  v-if="record.parentId != 0"
                  type="text"
                  @click="handleDelete(record)"
                  v-hasPermi="['system:dept:remove']"
              >
                <icon-delete/>
                删除
              </a-button>
            </template>
          </a-table-column>
        </template>
      </a-table>

      <!-- 添加或修改部门对话框 -->
     <a-drawer :visible="deptDrawerVisible" @ok="handleDeptOk" @cancel="handleDeptCancel" unmountOnClose :width="540">
       <template #title>
         <h4>{{title}}</h4>
       </template>
       <a-form ref="deptRef" :model="form" :rules="rules" label-width="80px">
         <a-row>
           <a-col :span="24" v-if="form.parentId !== 0">
             <a-form-item label="上级部门" field="parentId">
               <el-tree-select
                   v-model="form.parentId"
                   :data="deptOptions"
                   :props="{ value: 'deptId', label: 'deptName', children: 'children' }"
                   value-key="deptId"
                   placeholder="选择上级部门"
                   check-strictly
               />
             </a-form-item>
           </a-col>
           <a-col :span="24">
             <a-form-item label="部门名称" field="deptName">
               <a-input v-model="form.deptName" placeholder="请输入部门名称" />
             </a-form-item>
           </a-col>
           <a-col :span="24">
             <a-form-item label="显示排序" field="orderNum">
               <a-input-number v-model="form.orderNum" controls-position="right" :min="0" />
             </a-form-item>
           </a-col>
           <a-col :span="24">
             <a-form-item label="负责人" field="leader">
               <a-input v-model="form.leader" placeholder="请输入负责人" maxlength="20" />
             </a-form-item>
           </a-col>
           <a-col :span="24">
             <a-form-item label="联系电话" field="phone">
               <a-input v-model="form.phone" placeholder="请输入联系电话" maxlength="11" />
             </a-form-item>
           </a-col>
           <a-col :span="24">
             <a-form-item label="邮箱" field="email">
               <a-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
             </a-form-item>
           </a-col>
           <a-col :span="24">
             <a-form-item label="部门状态">
               <a-radio-group v-model="form.status">
                 <a-radio
                     v-for="dict in sys_normal_disable"
                     :value="dict.value"
                     :label="dict.value"
                 >{{ dict.label }}</a-radio>
               </a-radio-group>
             </a-form-item>
           </a-col>
         </a-row>
       </a-form>
     </a-drawer>
   </div>
</template>

<script setup name="Dept">
import { listDept, getDept, delDept, addDept, updateDept, listDeptExcludeChild } from "@/api/system/dept";
import {getCurrentInstance, reactive, ref, toRefs} from "vue";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const deptList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const deptOptions = ref([]);
const isExpandAll = ref(true);
const refreshTable = ref(true);
const deptDrawerVisible = ref(false)
const direction = ref('rtl')

const data = reactive({
  form: {},
  queryParams: {
    deptName: undefined,
    status: undefined
  },
  rules: {
    parentId: [{ required: true, message: "上级部门不能为空", trigger: "blur" }],
    deptName: [{ required: true, message: "部门名称不能为空", trigger: "blur" }],
    orderNum: [{ required: true, message: "显示排序不能为空", trigger: "blur" }],
    email: [{ type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }],
    phone: [{ pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }]
  },
});

const { queryParams, form, rules } = toRefs(data);

const handleDeptOk = () => {
  submitForm()
};
const handleDeptCancel = () => {
  deptDrawerVisible.value = false;
}

/** 查询部门列表 */
function getList() {
  loading.value = true;
  listDept(queryParams.value).then(response => {
    deptList.value = proxy.handleTree(response.data, "deptId");
    console.log(deptList.value)
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  deptDrawerVisible.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    deptId: undefined,
    parentId: undefined,
    deptName: undefined,
    orderNum: 0,
    leader: undefined,
    phone: undefined,
    email: undefined,
    status: "0"
  };
  proxy.resetForm("deptRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  listDept().then(response => {
    deptOptions.value = proxy.handleTree(response.data, "deptId");
  });
  if (row != undefined) {
    form.value.parentId = row.deptId;
  }
  deptDrawerVisible.value = true;
  title.value = "添加部门";
}
/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  listDeptExcludeChild(row.deptId).then(response => {
    deptOptions.value = proxy.handleTree(response.data, "deptId");
  });
  getDept(row.deptId).then(response => {
    form.value = response.data;
    deptDrawerVisible.value = true;
    title.value = "修改部门";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["deptRef"].validate(valid => {
    if (!valid) {
      if (form.value.deptId != undefined) {
        updateDept(form.value).then(response => {
          proxy.$message.success("修改成功");
          deptDrawerVisible.value = false;
          getList();
        });
      } else {
        addDept(form.value).then(response => {
          proxy.$message.success("新增成功");
          deptDrawerVisible.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$confirm('是否确认删除名称为"' + row.deptName + '"的数据项?').then(function() {
    return delDept(row.deptId);
  }).then(() => {
    getList();
    proxy.$message.success("删除成功");
  }).catch(() => {});
}

getList();
</script>
